package com;

import com.action.UserAction;
import com.bean.MyBean;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;





/*@SpringBootApplication是以下3个注解的整合:
 * @Configuration
 * @EnableAutoConfiguration
 * @ComponentScan
 * */
@SpringBootApplication

@ComponentScan("com.*")
/*
@Controller
是让URL请求返回具体的页面，如返回html、jsp页面等
*/

@RestController
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

    @RequestMapping("/hello")
    public String hello() {
        return "Hello World!";
    }

    /*模拟一个用户*/
    @GetMapping("user")
    public Map<String, Object> getUser() {
        HashMap<String, Object> user = new HashMap<>();
        user.put("id", 1);
        user.put("name", "张三");
        user.put("age", 18);

        return user;
    }


    /*获取参数*/
    @GetMapping("param")
    public String param(
            @RequestParam("id") String id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "age", required = false) Integer age) {
        String data = "";
        try {
            data = "id" + id +",name="+name +",age="+age;
        } catch (Throwable throwable)
        {
            data =  throwable.getMessage();
        }

        return data;
    }

    @GetMapping(value = "path/{id}/{name}")
    public String pathVar(
            @PathVariable("id") String id,
            @PathVariable(value = "name",required = false) String userName
    ){
        return "id="+id+",userName="+userName;
    }


    @RequestMapping(value = "json",method = RequestMethod.GET)
    public Map<String,Object>getJson(){
        HashMap<String, Object> user = new HashMap<>();
        user.put("id", 1);
        user.put("name", "2223");
        user.put("age", 22);
        return user;
    }

    @Autowired
    MyBean bean;
    @GetMapping("createBean")
    @CrossOrigin
    public void createBean(){
        /*注册bean*/
        String beanName = "MyBean";
        UserAction userAction = bean.getApplicationContext().getBean(beanName, MyBean.class);
        System.out.println(bean.getApplicationContext().getBean(beanName, MyBean.class).getClass().getName());
        String out = userAction.display();
        System.out.println("手工获取bean==》"+out);
    }

    /**
     * 调用自定义的Bean
     */
    @Resource(name = "userAction1")
    UserAction userAction1;

    /**
     * 另一种
     */
    @Autowired
    @Qualifier(value = "userAction1")
    UserAction userAction2;






}

